package view;

import java.awt.Color;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JLabel;
import javax.swing.Timer;

import org.jdesktop.swingx.JXStatusBar;

import control.MessageHandler;

import view.tables.IconizedInterface;


@SuppressWarnings("serial")
public class ExtendedJXStatusBar extends JXStatusBar implements Observer {
	private JLabel statusLabel = new JLabel(" ");
	private Timer timer;
	
	public ExtendedJXStatusBar(){
		add(statusLabel);
		MessageHandler.getInstance().addObserver(this);
	}

	@Override
	public void update(Observable arg0, Object arg1) {
		IconizedInterface msg = MessageHandler.getMessage();
		if(msg != null){
			statusLabel.setForeground(SystemColor.textText);
			statusLabel.setText(msg.toString());
			statusLabel.setIcon(msg.getIcon());
	
			timer = new Timer(500, new ActionListener() {
				Color from = SystemColor.textText;
				Color to = SystemColor.textInactiveText;
				int counter = 10;
				int deltaR = (to.getRed() - from.getRed())/counter;
				int deltaG = (to.getGreen() - from.getGreen())/counter;
				int deltaB = (to.getBlue() - from.getBlue())/counter;
				
				@Override
				public void actionPerformed(ActionEvent e) {
					if(counter-- > 0){
						from = new Color(from.getRed() + deltaR, from.getGreen() + deltaG, from.getBlue() + deltaB);
						statusLabel.setForeground(from);
						//statusLabel.setForeground(SystemColor.textInactiveText);
					} else {
						((Timer)e.getSource()).stop();
					}
				}
			});
			timer.start();
		}
	}

}
